#include "ScnsNrf24l01.c.h"

#if defined(SCNS_NRF24L01_ENABLE)&&SCNS_NRF24L01_ENABLE==1

#include "ScnsTimer.h"
#include "ScnsLed.h"
#if __SCNS_DEVICE_ID__==1
#define BSPS_PTX_SPI     SPI_1
#define BSPS_PTX_SCL     SPI1_SCK_B03
#define BSPS_PTX_MOSI    SPI1_MOSI_B05
#define BSPS_PTX_MISO    SPI1_MISO_B04
#define BSPS_PTX_CS      SPI1_NSS_A15
#define BSPS_PTX_IRQ     D3
#define BSPS_PTX_CE      C10
#elif __SCNS_DEVICE_ID__==2
#define BSPS_PTX_SPI     SPI_2
#define BSPS_PTX_SCL     SPI2_SCK_B10
#define BSPS_PTX_MOSI    SPI2_MOSI_B13
#define BSPS_PTX_MISO    SPI2_MISO_B12
#define BSPS_PTX_CS      SPI2_NSS_B15
#define BSPS_PTX_IRQ     B11
#define BSPS_PTX_CE      B14
#else
#error
#endif

const ScnsBspNrf24l01Config scnsBspNrf24l01Config[SCNS_NRF24L01_MAX]={
    {
        .rxAddr ={0X12,0X25,0XFF,0XFF,0XFF},
        .freq   =2525,
    }
};

ScnsStatus scnsBspNrf24l01Init(ScnsNrf24l01Enum nrfN)
{
    spi_init(BSPS_PTX_SPI,BSPS_PTX_SCL,BSPS_PTX_MOSI,BSPS_PTX_MISO,BSPS_PTX_CS,0,SystemCoreClock/48);
    gpio_init(BSPS_PTX_IRQ,GPI,GPIO_LOW,GPI_PULL_UP);
    gpio_init(BSPS_PTX_CE,GPO,GPIO_LOW,GPO_PUSH_PULL);
    exti_interrupt_init(BSPS_PTX_IRQ,EXTI_Trigger_Falling,6);
    return SCNS_STATUS_OK;
}

ScnsStatus scnsBspNrf24l01PostInit(ScnsNrf24l01Enum nrfN)
{
    return SCNS_STATUS_OK;
}

void scnsBspNrf24l01RW(ScnsNrf24l01Enum nrfN,uint8*val,uint32 num)
{
    scnsLedToggleAfter(SCNS_LED_D6,25*1000);
    spi_mosi(BSPS_PTX_SPI,val,val,num);
}

uint8 scnsBspNrf24l01GetIrq(ScnsNrf24l01Enum nrfN)
{
    return gpio_get(BSPS_PTX_IRQ);
}

void scnsBspNrf24l01SetCe(ScnsNrf24l01Enum nrfN,uint8 val)
{
    gpio_set(BSPS_PTX_CE,val);
}

#endif
